﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class CompanyController : Controller
    {
        //
        // GET: /Company/

        public ActionResult Index()
        {
            return View();
        }

		/// <summary>
		/// get all companies
		/// </summary>
		/// <returns></returns>
        public ActionResult GetCompany()
        {
            CompanyContainer container = CompanyModels.GetCompanyArray();
            return Json(container, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// select company by id (used to get company info for populate edit form when company is selected)
        /// </summary>
        /// <returns>json array with one record</returns>
        public ActionResult GetCompanyById()
        {
            decimal idCompany = 0;
            Decimal.TryParse(Request.Params["Id"], out idCompany);

            CompanyContainer container = CompanyModels.GetCompanyArrayById(idCompany);

            return Json(container, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// edit company info
        /// </summary>
        /// <returns></returns>
        public ActionResult EditCompany()
        {
            CompanyModels company = new CompanyModels();

            decimal idCompany = 0;
            Decimal.TryParse(Request.Params["Id"], out idCompany);            

            decimal nrEmployees = 0;
            Decimal.TryParse(Request.Params["IdEmployee"], out nrEmployees);

            decimal turnover = 0;
            Decimal.TryParse(Request.Params["IdTurnover"], out turnover);

            company.IdCompany = idCompany;
            company.IdEmployee = nrEmployees;
            company.IdTurnover = turnover;
            company.CompanyName = Request.Params["CompanyName"];
            company.Cui = Request.Params["Cui"];


            //CompanyModels checkCompany = new CompanyModels();
            //checkCompany = CompanyModels.CheckCompany(company);

            //CompanyModels checkCompanyEdit = new CompanyModels();
            //checkCompanyEdit = CompanyModels.CheckCompanyEdit(company);

            string messageBack = null;

            try
            {
                if (idCompany == 0)
                {
                    /*if (checkCompany == null)
                    {
                        CompanyModels.AddCompany(company);
                    }
                    else
                    {
                        messageBack = "Compania exista deja";
                    }*/
					CompanyModels.AddCompany(company);
                }
                else
                {
                    if (idCompany != 0)
                    {
						CompanyModels.EditCompany(company);
                        /*if (checkCompanyEdit == null)
                        {
                           
                        }
                        else
                        {
							messageBack = "Compania exista deja";
                        }*/
                    }
                }
                return Json(new { success = true, description = messageBack });
            }
            catch (Exception ex)
            {
                if(ex.Message.Contains("unique key constraint"))

				{					
					messageBack= "Exista deja o inregistrare cu acelasi nume";
				}				
				return Json(new { success = true, description = messageBack });
            }
        }

    }
}
